<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2019/4/26
 * Time: 13:58
 */

namespace service;


use think\Db;
use think\Exception;

class MemberService
{
    /**
     * 会员账户类型
     */
    const MEMBER_TYPE_NORMAL=1;
    const MEMBER_TYPE_SUPPLIER=2;

    /**
     * 账户金额变动
     */
    const ACCOUNT_LOG_TYPE_PARENT=1;
    const ACCOUNT_LOG_TYPE_CONSUME=2;
    const ACCOUNT_LOG_TEXT=[
        self::ACCOUNT_LOG_TYPE_PARENT=>'上级变动',
        self::ACCOUNT_LOG_TYPE_CONSUME=>'消费'
    ];

    /**
     * 改变账户资金
     * @param $mid
     * @param int $point
     * @param int $type
     * @param string $remark
     * @return bool
     * @throws Exception
     */
    static function log_account_change($mid,$point=0,$type=self::ACCOUNT_LOG_TYPE_PARENT,$remark=''){
        if($point==0)return true;
        try{
            Db::startTrans();
            //扣除用户余额
            $member_data['point']=Db::raw('point+'.$point);
            Db::name('store_member')->where('id',$mid)->update($member_data);
            //变动记录
            $al_data['mid']=$mid;
            $al_data['point']=$point;
            $al_data['type']=$type;
            $al_data['remark']=$remark;
            $al_data['addtime']=time();
            Db::name('account_log')->insert($al_data);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            throw new Exception($e);
        }
        return true;
    }
}